﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Diagnostics;

using Coronox.SkyShell;
using SkyShellEx.IconOverlay.Interfaces;
using System.IO;

namespace SkyShellEx.IconOverlay
{
    [ComVisible(true)]
    [Guid("069B7FAB-4568-4DC7-AF94-E9A92A1830C0")]
    public class IconOverlay : IShellIconOverlayIdentifier
    {
        [ComRegisterFunction()]
        public static void RegisterServer(Type type)
        {
            ShellComRegister.RegisterShellIconOverlayIdentifier(typeof(IconOverlay));
        }

        [ComUnregisterFunction()]
        public static void UnregisterServer(Type type)
        {
            ShellComRegister.UnregisterShellIconOverlayIdentifier(typeof(IconOverlay));
        }


        int IShellIconOverlayIdentifier.IsMemberOf(string pwszPath, int dwAttrib)
        {
            //return fileName.ToLower().EndsWith(".exe");
            if ((dwAttrib & SkyShellEx.IconOverlay.Const.Winnt.FILE_ATTRIBUTE_REPARSE_POINT) > 0)
            {
                string r;
                if (Sky.CanStopSyncFolder(pwszPath,out r))
                    return 0;
                else
                    return 1;
            }
            else
                return 1;
        }

        void IShellIconOverlayIdentifier.GetOverlayInfo(IntPtr pwszIconFile, int cchMax, out int pIndex, out Enums.ISIOI pdwFlags)
        {
            string fileName = this.GetOverlayIconLocation();
            pIndex = this.GetOverlayIconIndex();
            pdwFlags = this.GetOverlayIconSource() == ShellIconOverlaySource.FromFile ? Enums.ISIOI.ISIOI_ICONFILE : Enums.ISIOI.ISIOI_ICONINDEX;

            // Der Dateiname muss direkt per Marshalling in den IntPtr geschrieben werden.
            byte[] bytes = System.Text.Encoding.Unicode.GetBytes(fileName);
            if (bytes.Length + 2 < cchMax)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    Marshal.WriteByte(pwszIconFile, i, bytes[i]);
                }

                // Um nämlich die abschließenden Null-Bytes zu schreiben
                Marshal.WriteByte(pwszIconFile, bytes.Length, 0);
                Marshal.WriteByte(pwszIconFile, bytes.Length + 1, 0);
            }
        }


        public void GetPriority(out int pPriority)
        {
            pPriority = 0;
        }


        public string GetOverlayIconLocation()
        {
            //get the full location of the assembly with DaoTests in it 
            string fullPath = System.Reflection.Assembly.GetAssembly(typeof(IconOverlay)).Location;

            //get the folder that's in 
            string theDirectory = Path.GetDirectoryName(fullPath);

            return theDirectory + @"\Resources\skydrive.ico";
        }


        public ShellIconOverlaySource GetOverlayIconSource()
        {
            return ShellIconOverlaySource.FromFile;
        }


        public int GetOverlayIconIndex()
        {
            return 1;
        }





    }

}
